module ApplicationHelper


	def time_str time
		time.strftime("%Y-%m-%d %H:%M:%S") if time
	end

	def boolean_label val
		return sanitize '<span class="label label-success">Yes</span>' if val == true
		return sanitize '<span class="label label-danger">No</span>'
	end

	def info_label info
		sanitize '<span class="label label-info">'+info+'</span>'
	end 

	def hello
		truncate(computer.auth_key,{:length=>10})
	end

	def folder_title folder
		return unless folder
		return link_to folders_folder_path(folder),:title=>folder.name,:class=>"" do 
				sanitize('<span class="glyphicon glyphicon-folder-close font-size-18"></span> ')+truncate(folder.name,{:length=>32})
		end
	end

	def doc_title doc,target='self'
		return link_to doc_path(:id=>doc.id),:title=>doc.name,:class=>"" do 
			sanitize('<span class="glyphicon glyphicon-file font-size-18"></span>'+ truncate(doc.name,{:length=>32})+'')
		end
	end

	def docs_folder_link_tag folder
		return unless folder || folder.is_folder
		return link_to(truncate(folder.name,{:length=>32}),docs_folder_path(folder))
	end

	def name_tag obj
		obj.nil? ? "" : obj.name
	end

	def user_tag user
		user.nil? ? "" : user.name
	end

	def creator_tag creator
		creator.nil? ? ""  : creator.name
	end
	

	def doc_size_tag size
		return "#{size} B" if size < 1024
		return "#{ sprintf('%.2f',size / 1024.0)} KB" if size < 1024*1024
		return "#{ sprintf('%.2f',size / 1024 / 1024.0)} MB" if size < 1024*1024*1024
		return "#{ sprintf('%.2f',size / 1024 / 1024 / 1024.0)} GB" 
	end

	def alert_tag notice,level="danger"
		return if notice.blank?
			level = ["success","info","warning","danger"].include?(level) ? level : "info"
			tag = '<div class="alert alert-'+level+' alert-dismissable">'
			tag += ' <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'
			#tag += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'
			tag += notice 
			tag += '</div>'
		return sanitize(tag)
	end
	
end
